using AndyPike.Unfuddle.Contracts;
using AndyPike.Unfuddle.Entities.Tickets;

namespace AndyPike.Unfuddle.Commands.Tickets
{
    /// <summary>
    /// Provides a command for returning a single ticket by id for a given project
    /// </summary>
    public class FindTicketById : IUnfuddleFindCommand<Ticket>
    {
        private readonly int projectId;
        private readonly int ticketId;

        /// <summary>
        /// Instantiates the FindTicketById command
        /// </summary>
        /// <param name="projectId">The id of the project that owns the ticket</param>
        /// <param name="ticketId">The id of the ticket to return</param>
        public FindTicketById(int projectId, int ticketId)
        {
            this.projectId = projectId;
            this.ticketId = ticketId;
        }

        /// <summary>
        /// Queries the Unfuddle server for a ticket with a given id within the given project
        /// </summary>
        /// <param name="service">The Unfuddle service to use when executing the command</param>
        /// <returns>Returns a ticket with the matching id</returns>
        public Ticket Execute(IUnfuddleService service)
        {
            GuardAgainst.ArgumentNull(service, "service");

            string url = service.BuildUrl("projects/{0}/tickets/{1}", projectId, ticketId);
            return service.InvokeRequest<Ticket>(url);
        }
    }
}